
package com.compuvest.precharge;

import com.compuvest.web.Basket;
import com.compuvest.web.Customer;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.DecimalFormat;

public class Transaction {

    private static final String AGENT = "Mozilla/5.0 (Windows; N; Win98; en-US; m14) Netscape6/6.0b1";

    private DecimalFormat number = new DecimalFormat("0.00");

    public Transaction() {
//	this();
    }

    public String sendInformation(Basket basket) {

//        System.out.println("Start");
	Customer cust = basket.getCustomer();
	if(cust == null) { return "cust=null"; }

	StringBuilder form = new StringBuilder(3000);
	try {
//	    form.append("type=moto");
	    form.append("merchant_id=1204321656066860");
	    form.append("&security_1=fflzEH04GC");
	    form.append("&security_2=FAdhtNCmrR");
//	    form.append("&test=1");
	    form.append("&ecom_billto_online_ip=").append(basket.getClickstream().getIPAddress());
	    form.append("&ecom_billto_postal_name_first=").append(URLEncoder.encode(cust.getBillingAddress().getFirstName(), "UTF-8"));
	    form.append("&ecom_billto_postal_name_last=").append(URLEncoder.encode(cust.getBillingAddress().getLastName(), "UTF-8"));
	    form.append("&ecom_billto_postal_street_line1=").append(URLEncoder.encode(cust.getBillingAddress().getAddress1(), "UTF-8"));
	    form.append("&ecom_billto_postal_street_line2=").append(URLEncoder.encode(cust.getBillingAddress().getAddress2(), "UTF-8"));
	    form.append("&ecom_billto_postal_city=").append(URLEncoder.encode(cust.getShippingAddress().getCity(), "UTF-8"));
	    form.append("&ecom_billto_postal_stateprov=").append(cust.getBillingAddress().getState());
	    form.append("&ecom_billto_postal_postalcode=").append(URLEncoder.encode(cust.getBillingAddress().getZip(), "UTF-8"));
	    form.append("&ecom_billto_postal_countrycode=").append(cust.getBillingAddress().getCountry());
	    form.append("&ecom_billto_telecom_phone_number=").append(URLEncoder.encode(cust.getBillingAddress().getPhone(), "UTF-8"));
	    form.append("&ecom_billto_online_email=").append(URLEncoder.encode(cust.getEmail(), "UTF-8"));
	    form.append("&ecom_transaction_amount=").append(number.format(basket.getGrandTotal()));
	    form.append("&ecom_payment_card_number=").append(cust.getCardNumber());
	    form.append("&ecom_payment_card_expdate_month=").append(cust.getCExpMonth());
	    form.append("&ecom_payment_card_expdate_year=20").append(cust.getCExpYears());
	    form.append("&invoice_number=").append(basket.getOrderNumber());
	    form.append("&product_description=");
	    form.append("&client_id=").append(basket.getOrderNumber());
	    form.append("&currency=USD");

	}
	catch(UnsupportedEncodingException ex) {
            System.out.println("Error:Transaction:sendInformation:" + ex.getMessage());
        }

//System.out.println(form.toString());
	StringBuilder result = new StringBuilder(16);
	try {
            URL url = new URL("https://api.precharge.net/charge/");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setAllowUserInteraction(false);
	    connection.setRequestProperty("User-Agent", AGENT);
	    connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
	    connection.setRequestProperty("Content-length", Integer.toString(form.length()));

	    // Write to stream
	    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
	    out.write(form.toString().getBytes());
	    out.flush();
	    out.close();

	    // Read stream
	    InputStream in = connection.getInputStream();
	    int ch;
	    while((ch = in.read()) != -1) {
		result.append((char)ch);
	    }
	    in.close();
	}
	catch(IOException e) {
            System.out.println("Error:Transaction:sendInformation:" + e.getMessage());
        }
        //System.out.println("\n" + result.toString() + "\nfinish");

	return result.toString();
    }
}
